/*
 * Copyright (C) 2010-2011 Comcast Cable.  All Rights Reserved.
 */
package com.hercules.android.receiver.handlers.events;

/**
 * <p>
 * Helper class for the component.
 * </p>
 * <p>
 * AndroidAssembly1 changed class name
 * <p>
 * <strong>Thread safety: </strong> This class is not mutable, so it is thread safe.
 * </p>
 *
 * @author jaindsonvs
 * @version 1.0
 */
public class EventsUtil {
    /**
     * <p>
     * Template for error messages for arguments null.
     * </p>
     */
    private static final String EXPECTED_ERROR_MES_FOR_ARGUMENT_NULL_TEMPLATE = "Argument \"%s\" can not be null.";

    /**
     * <p>
     * Template for error messages for fields null.
     * </p>
     */
    private static final String EXPECTED_ERROR_MES_FOR_OBJECT_NULL_TEMPLATE = "Object \"%s\" should not be null.";

    /**
     * <p>
     * Template for error messages for empty arguments.
     * </p>
     */
    private static final String EXPECTED_ERROR_MES_FOR_EMPTY_ARGUMENT_TEMPLATE = "Argument \"%s\" can not be empty.";

    /**
     * <p>
     * Private constructor.
     * </p>
     */
    private EventsUtil() {
        // do nothing
    }

    /**
     * <p>
     * Checks whether a given string is empty.
     * </p>
     *
     * @param string the string to be checked.
     * @return <tt>true</tt> if the string is empty, <tt>false</tt> otherwise.
     */
    public static boolean isEmpty(String string) {
        return string.trim().length() == 0;
    }

    /**
     * <p>
     * Checks whether the given Object is null.
     * </p>
     *
     * @param arg the argument to check
     * @param name the name of the argument
     * @throws IllegalArgumentException if the given Object is null
     */
    public static void checkArgumentNull(Object arg, String name) {
        if (arg == null) {
            throw new IllegalArgumentException(format(EXPECTED_ERROR_MES_FOR_ARGUMENT_NULL_TEMPLATE, name));
        }
    }

    /**
     * <p>
     * Checks whether the given Object is null.
     * </p>
     *
     * @param arg the argument to check
     * @param name the name of the argument
     * @throws IllegalArgumentException if the given Object is null
     */
    public static void checkObjectNull(Object arg, String name) {
        if (arg == null) {
            throw new IllegalArgumentException(format(EXPECTED_ERROR_MES_FOR_OBJECT_NULL_TEMPLATE, name));
        }
    }

    /**
     * <p>
     * Checks whether the given string is empty.
     * </p>
     *
     * @param arg the argument to check
     * @param name the name of the argument
     * @throws IllegalArgumentException if the given string is empty
     */
    public static void checkArgumentEmpty(String arg, String name) {
        if (isEmpty(arg)) {
            throw new IllegalArgumentException(format(EXPECTED_ERROR_MES_FOR_EMPTY_ARGUMENT_TEMPLATE, name));
        }
    }

    /**
     * <p>
     * Formats properly a given template and replace the objects as arguments for the template.
     * </p>
     *
     * @param template the template to be used.
     * @param objects the objects to be replaced on template.
     * @return the template formated with the objects properly set.
     */
    private static String format(String template, String... objects) {
        return String.format(template, (Object[]) objects);
    }
}
